﻿using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;


namespace WebApp
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddAuthentication(options =>
            {
                options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;

            }).AddCookie();

            // add StartupFilter
            services.AddSingleton<IStartupFilter, MyStartupFilter>();
            services.AddSingleton<IStartupFilter, MyStartupFilter2>();


            // add background service
            services.AddSingleton<Microsoft.Extensions.Hosting.IHostedService, MyHostedService>();
            services.AddSingleton<Microsoft.Extensions.Hosting.IHostedService, MyHostedService2>();

            // add DbTest Service, has move to Repository.RepositoryHostingStartup
            //services.AddScoped<DbTest>();


            // 添加Mvc所用的服务
            services.AddMvc();

            // 使用自定义的AuthenticationHandler
            //services.AddAuthenticationCore(options => options.AddScheme<MyAuthenticationHandler>("myScheme", "demo scheme"));
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, Microsoft.AspNetCore.Hosting.IHostingEnvironment env, Microsoft.AspNetCore.Hosting.IApplicationLifetime lifeTime)
        {

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                //app.usede
            }

            lifeTime.RegisterOnStarted(() => Console.WriteLine($"WebApp OnStarted Callback Invoke."));

            app.UseAuthentication();

            app.UseMvc();

            //app.UseMyMiddleWare();


        }
    }
}
